﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LYQ.BLL;
using WebManager;

namespace BaoJian.WebManager.Manager.KeyWord
{
    public partial class EditKey : System.Web.UI.Page
    {
        private string ColumnID;
        private string selectCB;
        private LYQ.BLL.Keyword bll = new Keyword();
        protected void Page_Load(object sender, EventArgs e)
        {
           if(!Page.IsPostBack)
           {
               if (Session["EditKeys"] == null)
               {
                   Msg.Text = "Session出错，请返回重新选择要修改的关键字";
                   return;
               }
               selectCB = HttpContext.Current.Session["EditKeys"] as string;
               Session["EditKeys"] = null;
               ViewState["selectCB"] = selectCB;
               BindData();
           }
        }

        private  void BindData()
        {
            
            string[] keys = selectCB.Split(',');
            string where = " SID=" + keys[0];
            for(int i = 1; i < keys.Length;i++)
            {
                where += " or SID=" + keys[i];
            }

            IList<LYQ.Model.Keyword> list = bll.GetModelList(where);
            foreach (LYQ.Model.Keyword keyword in list)
            {
                Keys.Text += keyword.KWord + "\r\n";
                KLevels.Text += keyword.KLevel + "\r\n";
            }
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            string[] k = Keys.Text.Trim().Replace("\r\n", "#").Split('#');
            string[] level = KLevels.Text.Trim().Replace("\r\n", "#").Split('#');


            selectCB = ViewState["selectCB"] as string;
            string[] keys = selectCB.Split(',');
            for (int i = 0; i < keys.Length; i++)
            {
                if(k.Length == 0)
                    continue;
                int sid = Convert.ToInt32(keys[i]);
                LYQ.Model.Keyword model = bll.GetModel(sid);
                model.KLevel = Convert.ToInt32(level[i].Trim());
                model.KWord = k[i].Trim();
                //model.CDate = DateTime.Now;
                model.UDate = DateTime.Now;
                //model.HitCount = 0;
                //model.RepeatCount = 0;
                //model.Description = "";
                //model.RelatedColList = "";
                //model.RelatedDocKey = "";
                //model.RelatedAdKey = "";
                bll.Update(model);
            }
            ReturnToList();
          
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            ReturnToList();
        }

        private void ReturnToList()
        {
            string url = URLUtility.UrlBase + @"/WebManager/manager/KeyWord/List.aspx?OID=" + ColumnID;
            Page.Response.Redirect(url);
        }
    }
}
